﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MapHello.Service.Live;
using MapHello.Service;
using MapHello.Core;
using Microsoft.Live;
using MapHello.Data;

namespace MapHello
{
    public partial class StartPage : PhoneApplicationPage
    {
        private MapHelloService Service { get; set; }

        public StartPage()
        {
            InitializeComponent();



            Service = new MapHelloService();
            this.Loaded += OnStartPageLoaded;
        }

        async void OnStartPageLoaded(object sender, RoutedEventArgs e)
        {
            var securityContext = Service.LoadSecurityContext();
            var me = Service.LoadMe();
            Service.FakeConfiguration();
            if (null == securityContext
                || (DateTime.Now - securityContext.TokenExpired).TotalHours <= 2
                || null == me)
            {
                progressBar.Visibility = System.Windows.Visibility.Visible;
                statusText.Text = "login...";
                var client = new LiveAuthClient(Constants.WindowsLiveClientId);
                LiveLoginResult result = await client.LoginAsync(LiveService.Scopes);
                securityContext = new SecurityContext()
                {
                    Token = result.Session.AccessToken,
                    TokenExpired = result.Session.Expires.DateTime
                };
                Service.SaveSecurityContext(securityContext);

                statusText.Text = "loading user profile";
                Service.LoadLivePerson(OnLoadPersonCompletion,
                    new LoadPersonContext()
                    {
                        Token = result.Session.AccessToken,
                        PersonId = string.Empty
                    });
            }
            else
            {
                Service.StartSystemTask();
                Controller.Navigate(this, new Uri("/MapPage.xaml", UriKind.Relative), true);
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
        }

        #region delegate methods

        public void OnLoadPersonCompletion(LoadLivePersonArgs args)
        {
            statusText.Text = "Welcome " + args.Person.Name;
            var me = Service.LoadMe();
            if (null == me || me.Id == args.Person.Id)
            {
                //View.Instance.Me = args.Person;
            }
            else
            {
                MessageBox.Show("You're not login as the original user. The user data would be initialized!");
                Service.InitializeUserData();
                //View.Instance.Me = args.Person;
            }
            Service.SavePerson(args.Person);
            Service.StartSystemTask();

            Controller.Navigate(this, new Uri("/MapPage.xaml", UriKind.Relative), true);
        }
        #endregion
    }
}